<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span style="color:red"><b>可口可乐公司The Coca-Cola Company</b></span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="upAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="list" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <i class="el-icon-s-platform" style="color:#f14f5e" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="upAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEup(data.id)">修改子部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.pid" @click.native="hDel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :visible.sync="showDialog" :title="isedit?'编辑部门':'新增部门'" :close-on-click-modal="false" :close-on-press-escape="false">
        <!-- 子组件 -->
        <deparDialog v-if="showDialog" :isedit="isedit" :i-eid="iEid" :origon-list="origonList" @suesser="hSue" @hsuesser="hsuesser" @hCancel="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDateDepartments } from '@/api/departments' // 获取所有部门
import { getTreedepar } from '@/utils/getTreedepar.js'
import deparDialog from './deparDialog.vue'
export default {
  components: {
    deparDialog
  },
  data() {
    return {
      origonList: [], // 存储新的数组 - 部门所有信息
      isedit: false, // true代表编辑部门 || false代表添加部门
      iEid: '', // 获取id传入子组件
      showDialog: false,
      // 依赖一份树形数据
      list: []
    }
  },
  created() {
    this.logDepartments()
  },
  methods: {
    // 获取所有部门
    async logDepartments() {
      try {
        const { data: res } = await getDepartments()
        // console.log(res)
        res.data.depts.shift()
        this.list = getTreedepar(res.data.depts)
        this.origonList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
      } catch (error) {
        // 失败
      }
    },
    // 添加子部门
    upAdd(id) {
      this.isedit = false
      this.iEid = id
      this.showDialog = true
    },
    // 编辑子部门
    hEup(id) {
      this.isedit = true
      this.iEid = id
      this.showDialog = true
    },

    // 子传父弹框确认
    hSue() {
      this.showDialog = false
      this.logDepartments()
    },
    // 父传子修改
    hsuesser() {
      this.showDialog = false
      this.logDepartments()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDateDepartments(id)
        this.logDepartments()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}

</script>

<style>

</style>
